<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Creates a new DatePeriod object</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.dateperiod.html">« DatePeriod</a></li>
      <li style="float: right;"><a href="dateperiod.getdateinterval.html">DatePeriod::getDateInterval »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.dateperiod.html">DatePeriod</a></li>
    <li>Creates a new DatePeriod object</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="dateperiod.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DatePeriod::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DatePeriod::__construct</span> &mdash; <span class="dc-title">Creates a new DatePeriod object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateperiod.construct-description">
  <h3 class="title">说明</h3>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DatePeriod::__construct</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$start</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <code class="parameter">$interval</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$recurrences</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span><br>)</div>

  <div class="constructorsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><strong>DatePeriod::__construct</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$start</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <code class="parameter">$interval</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$end</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span><br>)</div>

  <div class="constructorsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><strong>DatePeriod::__construct</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$isostr</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span>)</div>

  <p class="para rdfs-comment">
   Creates a new DatePeriod object.
  </p>
  <p class="para">
   <span class="classname"><a href="class.dateperiod.html" class="classname">DatePeriod</a></span> objects can be used as an iterator to
   generate a number of <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span> or
   <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> object from a <code class="parameter">start</code>
   date, a <code class="parameter">interval</code>, and an <code class="parameter">end</code>
   date or the number of <code class="parameter">recurrences</code>.
  </p>
  <p class="para">
   The class of returned objects is equivalent to the
   <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span> or <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span>
   ancestor class of the <code class="parameter">start</code> object.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateperiod.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">start</code></dt>

     <dd>

      <p class="para">
       The start date of the period. Included by default in the result set.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">interval</code></dt>

     <dd>

      <p class="para">
       The interval between recurrences within the period.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">recurrences</code></dt>

     <dd>

      <p class="para">
       The number of recurrences. The number of returned results is
       one higher than this, as the start date is included in the result set
       by default. Must be greater than <code class="literal">0</code>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">end</code></dt>

     <dd>

      <p class="para">
       The end date of the period. Excluded by default from the result set.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">isostr</code></dt>

     <dd>

      <p class="para">
       A subset of the <a href="http://en.wikipedia.org/wiki/Iso8601#Repeating_intervals" class="link external">&raquo;&nbsp;ISO 8601 repeating
       interval specification</a>.
      </p>
      <p class="para">
       Examples of some ISO 8601 interval specification features that PHP does
       not support are:
      </p>
      <ol type="1">
       <li class="listitem">
        <span class="simpara">
         zero occurrences (<code class="literal">R0/</code>)
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         time offsets other than UTC (<code class="literal">Z</code>), such as <code class="literal">+02:00</code>.
        </span>
       </li>
      </ol>
     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       A bit field which can be used to control certain behaviour with start-
       and end- dates.
      </p>
      <p class="para">
       With <strong><code>DatePeriod::EXCLUDE_START_DATE</code></strong> you
       exclude the start date from the set of recurring dates within the
       period.
      </p>
      <p class="para">
       With <strong><code>DatePeriod::INCLUDE_END_DATE</code></strong> you
       include the end date in the set of recurring dates within the
       period.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-dateperiod.construct-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.2.0</td>
       <td>
        The <strong><code>DatePeriod::INCLUDE_END_DATE</code></strong> constant has been added.
       </td>
      </tr>

      <tr>
       <td>7.2.19, 7.3.6, 7.4.0</td>
       <td>
        <code class="parameter">recurrences</code> must be greater than <code class="literal">0</code> now.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-dateperiod.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2061">
    <p><strong>示例 #1 DatePeriod example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$start&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2012-07-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P7D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$end&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2012-07-31'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$recurrences&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$iso&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'R4/2012-07-01T00:00:00Z/P7D'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;All&nbsp;of&nbsp;these&nbsp;periods&nbsp;are&nbsp;equivalent.<br /></span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">(</span><span style="color: #0000BB">$start</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$recurrences</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">(</span><span style="color: #0000BB">$start</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$end</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">(</span><span style="color: #0000BB">$iso</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;By&nbsp;iterating&nbsp;over&nbsp;the&nbsp;DatePeriod&nbsp;object,&nbsp;all&nbsp;of&nbsp;the<br />//&nbsp;recurring&nbsp;dates&nbsp;within&nbsp;that&nbsp;period&nbsp;are&nbsp;printed.<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
2012-07-01
2012-07-08
2012-07-15
2012-07-22
2012-07-29
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2062">
    <p><strong>示例 #2 DatePeriod example with <strong><code>DatePeriod::EXCLUDE_START_DATE</code></strong></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$start&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2012-07-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P7D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$end&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2012-07-31'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">(</span><span style="color: #0000BB">$start</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$end</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">::</span><span style="color: #0000BB">EXCLUDE_START_DATE</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;By&nbsp;iterating&nbsp;over&nbsp;the&nbsp;DatePeriod&nbsp;object,&nbsp;all&nbsp;of&nbsp;the<br />//&nbsp;recurring&nbsp;dates&nbsp;within&nbsp;that&nbsp;period&nbsp;are&nbsp;printed.<br />//&nbsp;Note&nbsp;that,&nbsp;in&nbsp;this&nbsp;case,&nbsp;2012-07-01&nbsp;is&nbsp;not&nbsp;printed.<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
2012-07-08
2012-07-15
2012-07-22
2012-07-29
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2063">
    <p><strong>示例 #3 DatePeriod example showing all last Thursdays in a year</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$begin&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2021-12-31'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$end&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2022-12-31&nbsp;23:59:59'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromDateString</span><span style="color: #007700">(</span><span style="color: #DD0000">'last&nbsp;thursday&nbsp;of&nbsp;next&nbsp;month'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">(</span><span style="color: #0000BB">$begin</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$end</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DatePeriod</span><span style="color: #007700">::</span><span style="color: #0000BB">EXCLUDE_START_DATE</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$period&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'l&nbsp;Y-m-d'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Thursday 2022-01-27
Thursday 2022-02-24
Thursday 2022-03-31
Thursday 2022-04-28
Thursday 2022-05-26
Thursday 2022-06-30
Thursday 2022-07-28
Thursday 2022-08-25
Thursday 2022-09-29
Thursday 2022-10-27
Thursday 2022-11-24
Thursday 2022-12-29
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-dateperiod.construct-notes">
  <h3 class="title">注释</h3>
  <p class="simpara">
   Unbound numbers of repetitions as specified by ISO 8601 section 4.5
   &quot;Recurring time interval&quot; are not supported, i.e. neither passing
   <code class="literal">&quot;R/...&quot;</code> as <code class="parameter">isostr</code> nor passing
   <strong><code>null</code></strong> as <code class="parameter">end</code> would work.
  </p>
 </div>


</div></div></div></body></html>